package com.yx.cloud.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.gson.Gson;
import com.iot.base.service.req.FeedBackParam;
import com.iot.sweeper.AppContext;
import com.yx.cloud.R;
import com.yx.cloud.adapter.FeedBackDetailsAdapter;
import com.yx.cloud.base.BaseActivity;
import com.iot.base.bean.account.UserFeedBack;
import com.yx.cloud.databinding.FeedbackdetailsActivityBinding;
import com.yx.cloud.utils.Intent_utils;
import com.yx.cloud.utils.RomUtils;
import com.yx.cloud.utils.ToastUtil;
import com.iot.common.utils.Event;

import java.util.ArrayList;
import java.util.List;

import org.greenrobot.eventbus.EventBus;import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 反馈详情界面（会话）
 */
public class FeedbackDetailsActivity extends BaseActivity implements View.OnClickListener {
    private final static String TAG = "FeedbackDetailsActivity";
    private String feedbackId;
    private int feedbackType;
    private FeedBackDetailsAdapter adapter;
    private List<UserFeedBack> result;
    private List<UserFeedBack> mDetailsDateList;
    private FeedbackdetailsActivityBinding binding;

    @Override
    public void onCreate() {
        binding = FeedbackdetailsActivityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        if (!EventBus.getDefault().isRegistered(this)) {//加上判断
            EventBus.getDefault().register(this);
        }
        initData();
    }

    private void initData() {
        mDetailsDateList = new ArrayList<>();
        feedbackId = getIntent().getStringExtra("hdid");
        feedbackType = getIntent().getIntExtra("hdType", FeedBackParam.TYPE_APP);
        //线性布局
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.rvFeedBackDetails.setLayoutManager(linearLayoutManager);
        adapter = new FeedBackDetailsAdapter(this, mDetailsDateList);
        binding.rvFeedBackDetails.setAdapter(adapter);

        binding.ivFeedbackDetailReturn.setOnClickListener(this);
        binding.ivFeedBackContinueQuestion.setOnClickListener(this);

    }

    @Override
    protected void onStart() {
        super.onStart();
        FeedBackParam param =FeedBackParam.newBuilder().option(FeedBackParam.GET_TYPE_BY_ID).feedbackId(feedbackId).type(feedbackType).build();
        AppContext.getAccountService().getFeedback(param);
    }

    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        switch (v.getId()) {
            case R.id.iv_feedbackDetailReturn:
                Intent_utils.backIntent(this);
                break;
            case R.id.iv_FeedBack_ContinueQuestion:
                //继续提问
                Intent intent = new Intent(this, FeedbackMessageActivity.class);
                intent.putExtra("hdType", feedbackType);
                intent.putExtra("hdid", feedbackId);
                Log.i(TAG, " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 3 : " + feedbackType);
                startActivity(intent);
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Event event) {
        switch (event.getType()) {
            case Event.MESSAGE_TYPE_FEEDBACK_DETAILS_LIST:
                if (event.isResult()) {
                    result = (List<UserFeedBack>) event.getContent();
                    if (result.size() > 0) {
                        mDetailsDateList.clear();
                    }
                    mDetailsDateList.addAll(result);
                    Log.i(TAG, "查询反馈会话列表2222 : " + new Gson().toJson(result));
                    adapter.notifyDataSetChanged();
                } else {
                    ToastUtil.show(FeedbackDetailsActivity.this, event.getContent() + "", Toast.LENGTH_SHORT);
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
